import json
from threading import Timer
import re
from socket import *
import datetime

'''
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
        prefix="localhost_access_log" suffix=".txt"
        pattern="%h %l %u @[%{yyyy-MM-dd HH:mm:ss}t]@ %{X-Real_IP}i @ %r @ %s @ %b @%D@ %F" />
'''

point = 0
config = json.loads(open(r'./checkFile.json', encoding='utf-8').read())
file = config["file"]
log = config["logPath"]


def doMain(inc):
    print("检索更新的请求日志中······")
    global point
    ReallyFile = file + getDate() + '.txt'
    f = open(ReallyFile, encoding='utf-8')
    f.seek(point)
    lines = f.readlines()
    point = f.tell()
    file2 = open(log + getNow() + '.log', "a")
    if len(lines) > 0:
        file2.write("接受了请求：" + str(len(lines)) + "次\n")
    else:
        file2.write("这一秒没有接收请求\n")
    file2.close()
    t = Timer(inc, doMain, (inc,))
    t.start()


def getDate():
    return str(datetime.datetime.now().strftime('%Y-%m-%d'))


def getNow():
    return str(datetime.datetime.now().strftime('%Y-%m-%d %H_%M'))


def init():
    global point
    ReallyFile = file + getDate() + '.txt'
    f = open(ReallyFile, encoding='utf-8')
    f.seek(point)
    f.readlines()
    point = f.tell()


init()

doMain(5)
